﻿using System;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using SDDP;

namespace DotNetStar.TheKing.Domain.EventHandlers
{
    public class UserOperationEventHandler : EventHandlerBase<UserOperationEventHandler>,IEventHandler<UserOperationEvent>
    {
        IStorage<UserOperationHistory, Guid> _userOperationHistoryStorage;
        
        public UserOperationEventHandler(
            IStorage<UserOperationHistory, Guid> userOperationHistoryStorage,
            ILoggerFactory loggerFactory, 
            IMemoryCache memoryCache) 
            : base(loggerFactory, memoryCache)
        {
            _userOperationHistoryStorage = userOperationHistoryStorage;
        }

        public void Handle(UserOperationEvent e)
        {
            _userOperationHistoryStorage.Add(new UserOperationHistory
            {
                ObjectId = e.ObjectId,
                Remark = e.Remark,
                Type = e.OperationType,
                UserId = e.UserId
            });
            _userOperationHistoryStorage.Context.Commit();
        }
    }
}
